<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 角色模型
 *
 * @author www.dingwei.cn
 */

class AdmRoleModel extends MY_Model{
	
	private $tbRole = 'admin_role';
	
	/**
	 * 角色保存
	 * @param 	array 		$data
	 * @param 	integer 	$id
	 * @return	array
	 */
	public function save($data, $id = 0){
		if($id){
			$this->db->where('id', $id);
			$query = $this->update($this->tbRole, $data);
		}else{
			$query = $this->insert($this->tbRole, $data);
		}
		
		return $query;
	}
	
	/**
	 * 获取角色前端分页列表
	 * @return array
	 */
	public function getList(){
		
		$this->db->select('id,name,status')
				 ->order_by('id desc');
		$data = $this->pagination($this->tbRole);

		if( ! empty($data['data'])){
			foreach ($data['data'] as $k => $v){
				$data['aaData'][$k][] = $v['name'];
                $status = $v['status'] == 2 ? 1 : 2;
                $data['aaData'][$k][] = adminCreateBtn(array(
                    'label' => $this->getStatus($v['status'],true),
                    'attr' => array(
                        'href' => getUrl('setting/role/changeStatus?id='.$v['id'].'&status='.$status),
                        'alert-message' => "确认是否".$this->getStatus($status)."？",
                        'class' => 'ajax-request-btn deco',
                    ),
                    'tag' => 'a'
                ));
				$btn = adminCreateBtn(array(
                    'label' => ' 编辑',
                    'attr' => array(
                        'href' => getUrl('setting/role/edit?id='.$v['id']),
                        'class' => 'btn btn-xs blue-stripe default edit-btn'
                    ),
                    'tag' => 'a',
                    'permissions' => array(
                        'module' => 'setting',
                        'key' => 'role-edit'
                    )
                ));
				$btn .= adminCreateBtn(array(
                    'label' => '删除',
                    'attr' => array(
                        'href' => getUrl('setting/role/delete?id='.$v['id']),
                        'class' => 'btn btn-xs red-stripe default ajax-request-btn',
                        'alert-message'=>'确定删除角色 “ '.$v['name'].' ” ?',
                    ),
                    'tag' => 'a',
                    'permissions' => array(
                        'module' => 'setting',
                        'key' => 'role-delete'
                    )
                ));
                $data['aaData'][$k][] = $btn;
			}
		}
		unset($data['data']);
		return $data;
	}
	
	/**
	 * 获取角色详情
	 * @param 	integer 	$id
	 * @return	array
	 */
	public function getInfo($id){
		
		$data = array();
		if( ! $id) return $data;
		
		$this->db->select('id,name,status,permissions')
				 ->where('id', $id);
		$data = $this->getOne($this->tbRole);
		if( ! $data){
			$data = array();
		}else{
			$tA = json_decode($data['permissions'], true);
			if( ! $tA){
				$tA = array();
			}
			
			$data['permissions'] = $tA;
		}
		
		return $data;
	}
    /**
     * 删除
     *
     * @param string $id
     */
    public function deleteRole($id){

        if( ! $id) return false;
        $this->db->where('id', $id);
        return $this->delete($this->tbRole);
    }
	/**
	 * 获取状态
	 *
	 * @param	integer	$key
	 * @param	boolean	$show	是否显示
	 * @return	mixed
	 */
	public function getStatus($key = null, $show = false){
	
		$config = array(
						1 => '禁用',
						2 => '启用',
		);
	
		if($key !== null){
	
			if(isset($config[$key]) && ! $show){
				return $config[$key];
			}
	
			if(isset($config[$key]) && $show){
	
				if($key == 1){
					return '<span class="label label-danger">'.$config[$key].'</span>';
				}else if($key == 2){
					return '<span class="label label-success">'.$config[$key].'</span>';
				}
			}
			return '';
		}else{
			return $config;
		}
	
		return $config;
	}
	
	/**
	 * 获取所有的角色列表
	 */
	public function getAllList(){
		
		$this->db->select('id,name')
				 ->where('status', 2)
				 ->order_by('id desc');
		
		return $this->getAll($this->tbRole);
	}
}